package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 通过Id获取套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByID(Long id) {
        Setmeal setmeal= setmealMapper.getByID(id);

        List<SetmealDish> setmealDish=setmealDishMapper.getBySetmealID(id);
        SetmealVO setmealVO=new SetmealVO();
        setmealVO.setSetmealDishes(setmealDish);

        //前拷贝到后面（属性名字需要一致）
        BeanUtils.copyProperties(setmeal,setmealVO);

        setmealVO.setCategoryName(categoryMapper.getName(setmeal.getCategoryId()));
        return setmealVO;
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        List<SetmealDish> setmealDishs=new ArrayList<>();

        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealDishs=setmealDTO.getSetmealDishes();
        setmealMapper.save(setmeal);
        if (setmealDishs != null && !setmealDishs.isEmpty() &&setmeal.getId()!=null) {
            for (SetmealDish dish : setmealDishs) {
                // 统一设置 setmealId（示例）
                dish.setSetmealId(setmeal.getId());
            }
        }


        setmealDishMapper.save(setmealDishs);

    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        setmealDishMapper.deleteBatch(ids);
        setmealMapper.deleteBatch(ids);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {

        // 这行代码是关键：告诉 PageHelper 接下来的查询需要分页
        PageHelper.startPage(
                setmealPageQueryDTO.getPage(),// 页码（从1开始）
                setmealPageQueryDTO.getPageSize() // 每页条数
        );
        Page<DishVO> page= setmealMapper.pageQuery(setmealPageQueryDTO);
        // 从 Page 对象中提取总条数和当前页数据，封装成 PageResult
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());  // 总记录数
        pageResult.setRecords(page.getResult());  // 当前页的数据列表
        return pageResult;
    }

    @Override
    @Transactional
    public void modifySetmealwithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishs=setmealDTO.getSetmealDishes();

        setmealMapper.modify(setmeal);
        List<Long> a = new ArrayList<>();
        a.add(setmeal.getId());
        setmealDishMapper.deleteBatch(a);

        if (setmealDishs != null && !setmealDishs.isEmpty() &&setmeal.getId()!=null) {
            for (SetmealDish dish : setmealDishs) {
                // 统一设置 setmealId（示例）
                dish.setSetmealId(setmeal.getId());
            }
        }
        setmealDishMapper.save(setmealDishs);
    }

    @Override
    public void stopOrOpen(Setmeal setmeal) {
        setmealMapper.modify(setmeal);    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
